package com.biovr.backend.utils;

import com.biovr.backend.config.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by monodev on 2017/5/8.
 * HTTP 返回工厂类
 */
@Service
public class ResponseFactory<T> {
    /*
    返回自定义的RestResponse
     */

    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseFactory.class);

    public static <T> RestResponse<T> createRestResponse(boolean success,String msg,T t) {
        RestResponse<T> result = RestResponse.newInstance();
        result.setSuccess(success);
        result.setData(t);
        result.setMsg(msg);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("generate rest result:{}", result);
        }
        return result;
    }
    /**
     * success
     * @param t
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> createSuccessResult(T t) {

        return createRestResponse(true, "", t);
    }

    /**
     * error message
     * @param msg error message
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> createErrorResult(String msg) {

        return createRestResponse(false, msg,null );
    }

    /**
     * error
     * @param error error enum
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> createErrorResult(ErrorCode error) {

        return createErrorResult(error.getMessage());
    }
    /**
     * success no message
     * @return
     */
    public static RestResponse createSuccessResult() {
        return createSuccessResult(null);
    }
}
